/**
 * ReportGear(2011)
 */
package net.abbp.report.view.cell.editor;

import java.util.List;

import net.abbp.core.pattern.ListenerSupport;

/**
 * 抽象单元格编辑器
 * 
 * @version 1.0 2011-3-22
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since report 1.0
 */
public abstract class AbstractCellEditor implements ICellEditor {
    private ListenerSupport<CellEditorListener> lstnrSupport = new ListenerSupport<CellEditorListener>();
    private transient CellEditorEvent cellEditEvent = new CellEditorEvent(this);

    @Override
    public boolean stopCellEditing() {
        fireEditingStopped();

        return true;
    }

    @Override
    public void cancelCellEditing() {
        fireEditingCanceled();
    }

    @Override
    public void addCellEditorListener(CellEditorListener listener) {
        this.lstnrSupport.addListener(listener);
    }

    @Override
    public void removeCellEditorListener(CellEditorListener listener) {
        this.lstnrSupport.removeListener(listener);
    }

    /**
     * 触发编辑停止事件
     */
    protected void fireEditingStopped() {
        List<CellEditorListener> listeners = this.lstnrSupport.getListeners();

        for (CellEditorListener lstnr : listeners) {
            lstnr.editingStopped(this.cellEditEvent);
        }
    }

    /**
     * 触发事件取消事件
     */
    protected void fireEditingCanceled() {
        List<CellEditorListener> listeners = this.lstnrSupport.getListeners();

        for (CellEditorListener lstnr : listeners) {
            lstnr.editingCanceled(this.cellEditEvent);
        }
    }
}
